/*
* 此文件中只定义了post请求，若需get请求，请在文档最后自行定义。
* 严禁擅自修改或是删除此文件中原有方法，如遇问题请先联系提供者。
*/

import axios from 'axios'
import { Notice } from 'view-design'
import Vue from 'vue'
import VueRouter from 'vue-router'
import { routers } from '../router/routers'
Vue.use(VueRouter)
export const router = new VueRouter({
  routes: routers,
  mode: 'history'
})

/* eslint-disable */
function paramsFromJson (pSize, pNum, fromData) {
  let postData = {
    data: JSON.stringify({
      pSize: pSize,
      pNum: pNum,
      paramList: fromData
    })
  }
  return postData
}

function fromJson (fromData) {
  let postData = { 
      data: JSON.stringify(fromData)
  }
  return postData
}

// post请求封装
function getPost (apiName, params, callback) {
  axios.post(Vue.prototype.rootUrl + apiName, params).then((response) => {
    let all = response.data
    let code = response.data.code
    let data = response.data.data
    let message = response.data.message
    if (all === -1) {
      router.push({path: '/servWebMetering/login'})
      location.reload()
      localStorage.clear()
      sessionStorage.clear()
    } else {
      // 根据后端实际返回做修改
      if (code === 'E0') { // 成功
        if (callback) callback(data, all, code)
      } else { // 失败
        if (callback) callback(data, all, code)
        Notice.error({
          title: '请求失败了！',
          desc: message,
          duration: 5
        })
      }
    }
  }).catch((error) => {
    console.log(error)
  })
}

export { getPost, paramsFromJson, fromJson }

